<template>
  <div class="container">
    <get-user-info v-if="isGetUserInfo" pageType="cart" @updateUserInfo="initMemberInfo"></get-user-info>
    <template v-if="getAnchorType && getCustomerId">
      <div class="flex v-align-c cart-topbar font-13 font-color-6 pad-l-10 pad-r-10">
        <div class="flex v-align-c align-l height-30 pad-l-10 pad-r-10" @click="isExplain = true">
          <img class="width-17 height-17 mar-r-5" v-if="fileSpace" :src="fileSpace + '/images/icon-ques-6.png'"/>
          说明
        </div>
        <div class="flex-item"></div>
        <div @click="manageData">
          <div class="flex height-30 flex-center pad-l-30 pad-r-30">
            {{ !manageBoolean ? '管理' : '完成' }}
          </div>
        </div>
      </div>
      <div class="height-40"></div>
      <div class="panel" v-for="(item, index) in listData" :key="index" @click="goDetail(item)">
        <van-swipe-cell :right-width="54" @close="onClose(item,$event)" async-close>
          <div class="flex pad-l-15 pad-r-15 pad-t-12 pad-b-8">
            <div class="pad-r-10 check-box-l" @click.stop.prevent="onChangeOneSel(item, index)">
              <van-checkbox :value="item.show ? true: false" :checked-color="getAppInfo.style.color2"></van-checkbox>
            </div>
            <div class="mar-r-10">
              <img :src="item.goodsImage + '?imageView2/2/w/180'" class="img-item" />
            </div>
            <div class="flex-item mar-t--3">
              <div class="goods-title line-1 font-12">{{ item.goodsTitle }}</div>
              <div class="flex mar-r--15">
                <div class="flex-item font-color-9 mar-r--10">
                  <p class="mar-t--3 mar-b--2"><span class="font-11 pad-r-1">¥</span><span class="font-13">{{ item.income }}</span></p>
                  <p class="font-11">预计赚</p>
                </div>
                <div class="flex-item font-color-9">
                  <p class="font-12">¥{{ item.sellPrice }}</p>
                  <p class="font-11">观众购买价</p>
                </div>
                <div class="flex-item font-red">
                  <template v-if="item.specimenPrice * 1 !== 0 && getRoleType !== 2">
                    <p class="font-12">¥{{ item.specimenPrice }}</p>
                    <p class="font-11">样品价</p>
                  </template>
                  <template v-else>
                    <p class="font-13">免费寄样</p>
                  </template>
                </div>
              </div>
              <div class="flex v-align-c mar-t-8">
                <div class="goods-prop font-10" v-if="item.skuDetail" @click.stop="getSkuBuyEls(item)">
                  <div class="flex v-align-c">
                    <div class="goods-sku line-1">{{ item.skuDetail }}</div>
                    <icon class="iconfont icon-arrow-right font-10 font-color-9"></icon>
                  </div>                  
                </div>
                <div class="flex-item">&nbsp;</div>
                <div class="goods-num-bar flex flex-center" @click.stop="getSkuBuyEls(item)">
                  <div>×{{ item.goodsNum }}</div>
                </div>
              </div>
            </div>
          </div>
          <div slot="right">删除</div>
        </van-swipe-cell>
      </div>
      <div class="panel" v-for="(item, index) in promotionList" :key="index">
        <div class="height-39 border-b v-align-c pad-l-15 font-12">
          <div class="shipping-icon">包邮</div>
          <div class="mar-l-7 font-w">满{{item.fullPrice}}元包邮</div>
        </div>
        <div class="shipping-goods" v-for="(goods, goodsIndex) in item.feeGoodsList" :key="goodsIndex">
          <van-swipe-cell :right-width="54" @close="onClose(goods,$event)" async-close>
            <div class="flex pad-l-15 pad-r-15">
              <div class="pad-r-10 check-box-l" @click.stop.prevent="onChangeTwoSel(goods, goodsIndex)">
                <van-checkbox :value="goods.show ? true: false" :checked-color="getAppInfo.style.color2"></van-checkbox>
              </div>
              <div class="flex-item flex mar-t--3 pad-t-20 pad-b-20 goods-info">
                <div class="mar-r-10">
                  <img :src="goods.goodsImage + '?imageView2/2/w/180'" class="img-item" />
                </div>
                <div class="flex-item mar-t--3">
                  <div class="goods-title line-1 font-12">{{ item.goodsTitle }}</div>
                  <div class="flex v-align-c mar-r--15">
                    <div class="flex-item font-color-9 mar-r--20">
                      <p><span class="font-11 pad-r-1">¥</span><span class="font-13">{{ item.income }}</span></p>
                      <p class="font-11">预计赚</p>
                    </div>
                    <div class="flex-item font-color-9">
                      <p class="font-12">{{ item.sellPrice }}</p>
                      <p class="font-11">观众购买价</p>
                    </div>
                    <div class="flex-item font-red">
                      <template v-if="item.specimenPrice * 1 !== 0 || getRoleType !== 2">
                        <p class="font-12">¥{{ item.specimenPrice }}</p>
                        <p class="font-11">样品价</p>
                      </template>
                      <template v-else>
                        <p class="font-13">免费寄样</p>
                      </template>
                    </div>
                  </div>
                  <div class="flex v-align-c mar-t-8">
                    <div class="goods-prop font-10" v-if="item.skuDetail">
                      <div class="flex v-align-c">
                        <div class="goods-sku line-1">{{ item.skuDetail }}</div>
                        <icon class="iconfont icon-arrow-right font-10 font-color-9"></icon>
                      </div>                  
                    </div>
                    <div class="flex-item">&nbsp;</div>
                    <div class="goods-num-bar flex flex-center">
                      <div>×{{ item.goodsNum }}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div slot="right">删除</div>
          </van-swipe-cell>
        </div>
      </div>
      <!-- 失效商品 -->
      <template v-if="listFailData.length !== 0">
        <div class="fail-box">
          <div class="flex v-align-c pad-l-13">
            <h3 class="flex-item font-15 font-w pad-t-12">失效货品</h3>
            <div class="font-color-9 font-15 pad-t-12 pad-l-13 pad-r-13" @click="clearFailAll">清空</div>
          </div>
          <div class="panel" v-for="(item, index) in listFailData" :key="index">
            <van-swipe-cell :right-width="54" @close="onClose(item,$event)" async-close>
              <div class="flex pad-l-15 pad-r-15 pad-t-16 pad-b-8">
                <div class="mar-l--6 fail-tag-box">
                  <div class="fail-tag font-10 mar-r-4 width-30">失效</div>
                </div>
                <div class="mar-r-10">
                  <img :src="item.goodsImage + '?imageView2/2/w/180'" class="img-item" />
                </div>
                <div class="flex-item mar-t--3">
                  <div class="goods-title line-1 font-12 font-color-9">{{ item.goodsTitle }}</div>
                  <div class="font-12 pad-t-30 mar-t-13 font-color-9">商品已不能购买</div>
                </div>
              </div>
              <div slot="right">删除</div>
            </van-swipe-cell>
          </div>
        </div>
      </template>
      <div class="pad-26"></div>
      <div class="flex bottom-btn" :class="isIPhoneX ? 'pad-b-15' : ''">
        <div class="v-align-c check-all pad-l-10" @click="onCheckAll">
          <van-checkbox :value="checkAll" :checked-color="getAppInfo.style.color2">全选</van-checkbox>
        </div>
        <div class="v-align-c flex-item pad-l-12">
          <div class="width-per-100 mar-r-10 font-13 mar-b--2">
            合计：
            <label>¥ {{ total }}</label>
          </div>
        </div>
        <div v-if="!manageBoolean" class="v-align-c submit" @click="settlement">
          <div
          :class="'submit-btn-' + selCartList.length"
          class="align-c submit-btn flex-center width-per-100"
          :style="{
            backgroundColor: getAppInfo.style.color2,
            borderColor: getAppInfo.style.color2,
            color: getAppInfo.style.styleType === 10 ? getAppInfo.style.color4 : '#fff'}">
            <div>申请寄样({{number}})</div>
          </div>
        </div>
        <div v-else class="v-align-c submit" @click="removeCheckbox">
          <div class="align-c remove-btn flex-center width-per-100"
          :style="{
            backgroundColor: getAppInfo.style.color4,
            borderColor: getAppInfo.style.color2,
            color: getAppInfo.style.styleType === 10 ? getAppInfo.style.color2 : '#fff'}">
            <div>删除</div>
          </div>
        </div>
      </div>
      <explain-layer v-if="isExplain"></explain-layer>
      <!-- sku选择 -->
      <div v-if="loadinSkuComponent">
        <get-sku-buy
          ref="getSkuBuy"
          :goodsData="goods"
          :qty="qty"
          :room_id="room_id"
          @finished="getSkuBuyFinished"></get-sku-buy>
      </div>
    </template>
    <div v-if="noneData" class="align-c none-data">
      <img class="width-207 height-173" v-if="fileSpace" :src="fileSpace + '/images/none-data.png'"/>
      <p class="font-16 font-color-9 pad-t-5">暂无选品数据</p>
      <div v-if="(!getAnchorType || !getCustomerId) && getRoleType !== 2" class="regBtn flex flex-center" @click="regBtn">
        <span>立即注册</span>
      </div>
    </div>
    <identity-type v-if="anchorBoolean"></identity-type>
  </div>
</template>

<script>
import GetUserInfo from '@/components/GetUserInfo'
import GetSkuBuy from '@/components/GetSkuBuy'
import ExplainLayer from './components/ExplainLayer'
import identityType from '@/components/identityType'
import {
  deleteShoppingCart,
  getShopCart,
  getGoodsDetails,
  listGoodsUnitType,
  deleteNoEffective,
  doGetOrderCheck
} from '@/api/shop'
import { getMemberInfo } from '@/api/home'
import { setNickName, setAnchorType } from '@/utils/auth'
import { getAppInfo } from '@/api/common'
import {getAnchorApplyList} from '@/api/personal'

export default {
  components: { GetUserInfo, GetSkuBuy, ExplainLayer, identityType },
  data() {
    return {
      cartItem: {},
      loadinSkuComponent: false,
      goods: {},
      room_id: '',
      qty: '',
      listGoodsUnitType: [],
      manageBoolean: false,
      isExplain: false,
      isGetUserInfo: false,
      checkAll: false,
      listData: [],
      listDataCartIds: [], // 存放有效商品的cartid
      listFailData: [],
      listQuery: {
        customerId: '',
        enterpriseId: ''
      },
      isInitDone: false,
      selCartList: [],
      selCartData: [],
      allCartList: [],
      goodsType: null,
      shareMemberId: '',
      number: 0,
      type: null,
      spaceBarHeight: 0,
      promotionList: [], // 包邮活动商品数组
      fastGoods: [],
      getAppInfo: {},
      navList: [],
      noneData: false,
      delCartIds: [], // 删除的商品id
      anchorBoolean: false,
      promptText: '',
      platformObj: {},
      statusNum: null,
      goodsListData: [],
      anchorType: null
    }
  },
  computed: {
    isIPhoneX() {
      return this.$utils.isIPhoneX()
    },
    total() {
      let total = 0
      let totalPro = 0
      this.listData.forEach((item, index) => {
        this.selCartList.forEach((selItem, selIndex) => {
          if (item.cartId === selItem) {
            total = (total * 1 + item.specimenPrice * item.goodsNum).toFixed(2)
          }
        })
      })
      if (!this.$utils.isNumber(total)) total = 0
      return (Math.round(total * 100) / 100 + Math.round(totalPro * 100) / 100).toFixed(2)
    }
  },
  onLoad() {
    // 初始化data
    Object.assign(this.$data, this.$options.data())
    this.getAppInfo = this.$pt.getStorageSync('appInfo')
    this.spaceBarHeight = this.$pt.getSystemInfoSync()['statusBarHeight'] + 44 + 36
    this.getAppEls()
  },
  async onShow() {
    if (this.$pt.getStorageSync('loginCode') === 5001) {
      this.$pt.reLaunch({ url: '/pages/home/main' })
      return
    }
    await this.getAnchorApplyList()
    if (this.getMemberId) {
      this.shareMemberId = this.getCurrentPage().query.shareMemberId
      this.selCartList = []
      this.selCartData = []
      this.allCartList = []
      this.listDataCartIds = []
      this.number = 0
      this.listQuery.memberId = this.getMemberId
      this.listQuery.customerId = this.getCustomerId
      this.listQuery.enterpriseId = this.getEnterpriseId ? this.getEnterpriseId : ''
      this.listQuery.roleType = this.getRoleType ? this.getRoleType : ''
      if (this.getCustomerId) {
        this.getShopCart()
      }
    }
    if (!this.getAnchorType || !this.getCustomerId) {
      this.noneData = true
    }
  },
  methods: {
    getAnchorApplyList() {
      if (!this.getCustomerId) return
      return new Promise((resolve, reject) => {
        this.statusNum = null
        let data = {
          memberId: this.getCustomerId
        }
        getAnchorApplyList(data).then((response) => {
          if (response.isError) return
          let resData = response.data
          if (resData.length) {
            this.statusNum = -1
            resData.forEach(item => {
              this.$set(this.platformObj, item['relationPlatform'], {})
              this.$set(this.platformObj[item['relationPlatform']], 'applyStatus', item['applyStatus'])
              this.$set(this.platformObj[item['relationPlatform']], 'rejectReason', item['rejectReason'])
              if (item.applyStatus === 1) {
                this.statusNum = 1
              }
            })
          }
          if (this.statusNum === 1) {
            this.anchorType = 1
            setAnchorType(1)
          } else {
            this.anchorType = 0
          }
          resolve(true)
        }).catch(() => {
        })
      })
    },
    regBtn() {
      if (!this.getCustomerId) {
        this.isGetUserInfo = true
      } else {
        if (this.statusNum) {
          this.anchorBoolean = true
        } else {
          this.$pt.navigateTo({ url: '/pages/personal/apply-anchor/main?inPage=1' })
        }
      }
    },
    getMemberInfo() {
      if (this.getRoleType === 2) return
      getMemberInfo(this.getCustomerId).then((response) => {
        if (response.isError) return
        setNickName(response.data.nickname)
      }).catch(() => {
      })
    },
    getSkuBuyFinished(params) {
      this.goods = {}
      if (this.listData.length) {
        this.listData.forEach(item => {
          if (item.goodsId === params.goodsId) {
            item.goodsNum = params.goodsNum
          }
        })
      }
      if (this.promotionList.length) {
        this.promotionList.forEach(item => {
          if (item.feeGoodsList) {
            item.feeGoodsList.forEach(itemOne => {
              if (itemOne.goodsId === params.goodsId) {
                itemOne.goodsNum = params.goodsNum
              }
            })
          }
        })
      }
    },
    getSkuBuyEls(item) {
      this.$utils.showToast({
        title: '加载中',
        icon: 'loading'
      })
      this.goods = {}
      this.cartItem = {}
      this.cartItem = item
      let data = {
        goodsId: item.goodsId,
        customerId: this.getCustomerId,
        roleType: this.getRoleType ? this.getRoleType : ''
      }
      getGoodsDetails(data).then(response => {
        response.data.unitType = response.data.unitType ? response.data.unitType.toString() : '1'
        this.goods = response.data
        this.initListGoodsUnitType()
      })
    },
    // 价格单位
    initListGoodsUnitType() {
      listGoodsUnitType(this.getMemberId).then((response) => {
        if (response.isError) return
        this.listGoodsUnitType = this.$utils.objToArr(response.data, ['text', 'value'])
        this.listGoodsUnitType.forEach(item => {
          if (this.goods.unitType === item.value) {
            this.qty = item.text
          }
        })
        this.showSkuPanle()
      }).catch(() => {
      })
    },
    showSkuPanle() {
      const isCart = true
      const _that = this
      this.loadinSkuComponent = false
      setTimeout(() => {
        this.$pt.hideToast()
        this.loadinSkuComponent = true
        this.$nextTick(() => {
          _that.$refs.getSkuBuy.showSkuPanle(isCart, _that.cartItem)
        })
      }, 100)
    },
    goDetail(item) {
      this.$pt.navigateTo({ url: `/pages/shop/detail/main?goodsId=${item.goodsId}&goodsType=${item.goodsType}` })
    },
    clearFailAll() {
      const that = this
      this.$pt.showModal({
        content: '确定要清空失效商品列表吗？',
        success(res) {
          if (res.confirm) {
            deleteNoEffective({
              memberId: that.getMemberId,
              customerId: that.getCustomerId,
              roleType: that.getRoleType
            }).then((response) => {
              if (response.isError) return
              that.$utils.showToast({ title: '删除成功' })
              setTimeout(() => {
                that.getShopCart()
              }, 500)
            }).catch(() => {
            })
          } else if (res.cancel) {
            console.log('用户取消')
          }
        }
      })
    },
    removeCheckbox() {
      // console.log(this.delCartIds)
      if (!this.delCartIds.length) return
      this.deleteShoppingCart(this.delCartIds)
    },
    manageData() {
      this.manageBoolean = !this.manageBoolean
    },
    getAppEls() {
      getAppInfo().then((response) => {
        if (response.isError) return
        this.getAppInfo = response.data
        this.getAppInfo.style.color2 = '#7427ff'
        this.getAppInfo.style.color4 = '#ffffff'
        this.getAppInfo.style.styleType = 10
      }).catch(() => {
      })
    },
    initMemberInfo() {
      this.getMemberInfo()
    },
    getShopCart() {
      if (!this.anchorType && this.getRoleType !== 2) return
      this.goods = {}
      this.isInitDone = false
      this.listData = []
      this.listDataCartIds = []
      this.selCartList = []
      this.selCartData = []
      this.allCartList = []
      this.listFailData = []
      this.promotionList = []
      this.number = 0
      if (!this.getShowBuyCard) {
        this.listQuery.customerId = this.getMemberId
      }
      getShopCart(this.listQuery).then(response => {
        if (response.isError) return
        this.$pt.hideToast()
        if (response.data.promotionList) {
          this.promotionList = response.data.promotionList
        } else {
          this.promotionList = []
        }
        if (this.promotionList.length !== 0) {
          this.promotionList.forEach((itemOne, indexOne) => {
            if (itemOne.feeGoodsList) {
              itemOne.feeGoodsList.forEach((subItem, subIndex) => {
                subItem.show = false
                this.allCartList.push(subItem.cartId)
              })
            }
          })
        }
        if (!response.data.effectiveList) {
          response.data.effectiveList = []
        }
        if (!response.data.noEffectiveList) {
          response.data.noEffectiveList = []
        }
        this.noneData = false
        if (!response.data.effectiveList.length && !response.data.promotionList.length && !response.data.noEffectiveList.length) {
          this.noneData = true
        }
        let listRes = []
        if (response.data.effectiveList) {
          listRes = response.data.effectiveList
        }
        if (response.data.noEffectiveList) {
          this.listFailData = response.data.noEffectiveList // 失效商品
        }
        this.listData = []
        this.listDataCartIds = []
        this.$pt.removeStorageSync('fastGoods')
        this.fastGoods = []
        listRes.forEach((item, index) => {
          // 点击快递类型
          item.show = false
          this.listData.push(item)
          this.listDataCartIds.push(item.cartId)
          this.allCartList.push(item.cartId)
        })
        let allEffectiveList = []
        let promotionList = []
        this.promotionList.forEach(item => {
          item.feeGoodsList.forEach(itemSub => {
            promotionList.push(itemSub)
          })
        })
        allEffectiveList = listRes.concat(promotionList)
        if (allEffectiveList.length) {
          allEffectiveList.forEach((item, index) => {
            // 重新获取快速购买数据
            if (item.skuId) {
              // 多规格
              let skuData = []
              skuData.push({
                skuId: item.skuId,
                goodsNum: item.goodsNum,
                sellPrice: (Math.round((item.goodsNum * item.sellPrice) * 100) / 100).toFixed(2)
              })
              let goodsNum = null
              let totalSellPrice = null
              skuData.forEach(item => {
                goodsNum += item.goodsNum
                totalSellPrice += Math.round(item.sellPrice * 100) / 100
              })
              this.fastGoods.push({
                goodsId: item.goodsId,
                skuFlag: 1,
                skuList: skuData,
                goodsNum: goodsNum,
                totalSellPrice: (Math.round(totalSellPrice * 100) / 100).toFixed(2)
              })
            } else {
              // 单规格
              this.fastGoods.push({
                goodsId: item.goodsId,
                skuFlag: 0,
                goodsNum: item.goodsNum,
                sellPrice: (Math.round((item.goodsNum * item.sellPrice) * 100) / 100).toFixed(2)
              })
            }
          })
        }
        let map = {}
        let dest = []
        this.fastGoods.forEach(item => {
          if (!map[item.goodsId]) {
            if (item.skuFlag) {
              dest.push({
                goodsId: item.goodsId,
                goodsNum: item.goodsNum,
                totalSellPrice: item.totalSellPrice,
                skuFlag: 1,
                skuList: item.skuList
              })
            } else {
              dest.push({
                goodsId: item.goodsId,
                goodsNum: item.goodsNum,
                sellPrice: item.sellPrice,
                skuFlag: 0,
                skuList: item.skuList
              })
            }
            map[item.goodsId] = item
          } else {
            dest.forEach(itemSub => {
              if (itemSub.goodsId === item.goodsId) {
                itemSub.goodsNum = itemSub.goodsNum + item.goodsNum
                itemSub.totalSellPrice = (Math.round(itemSub.totalSellPrice * 100) / 100 + Math.round(item.totalSellPrice * 100) / 100).toFixed(2)
                itemSub.skuList = itemSub.skuList.concat(item.skuList)
              }
            })
          }
        })
        this.fastGoods = []
        this.fastGoods = dest
        // 重新获取购物车角标数
        this.resetBadge()
        this.allCartList = this.allCartList.sort(this.sortNumber)
        this.isInitDone = true
        this.checkAll = false
      }).catch(() => {
      })
    },
    // 判断两个数组相等
    compareArraySort(a1, a2) {
      if ((!a1 && a2) || (a1 && !a2)) return false
      if (a1.length !== a2.length) return false
      a1 = [].concat(a1)
      a2 = [].concat(a2)
      a1 = a1.sort()
      a2 = a2.sort()
      for (var i = 0, n = a1.length; i < n; i++) {
        if (a1[i] !== a2[i]) return false
      }
      return true
    },
    resetBadge() {
      if (this.fastGoods.length) {
        let cartBadge = 0
        this.fastGoods.forEach(itemGoods => {
          cartBadge += itemGoods.goodsNum
        })
        if (this.cartIndexEls()) {
          this.$pt.setTabBarBadge({
            index: this.cartIndexEls(),
            text: String(cartBadge)
          })
        }
      } else {
        this.$pt.removeStorageSync('fastGoods')
        this.$pt.removeTabBarBadge({
          index: 1
        })
      }
    },
    // 普通商品
    onChangeOneSel(item, index) {
      // 判断是否能一起结算
      if (item.show) {
        this.selCartList.forEach((selItem, selIndex) => {
          if (selItem === item.cartId) {
            this.selCartList.splice(selIndex, 1)
          }
        })
      } else {
        this.selCartList.push(item.cartId)
      }
      item.show = !item.show
      this.number = this.selCartList.length
      if (this.compareArraySort(this.listDataCartIds, this.selCartList)) {
        this.checkAll = true
      } else {
        this.checkAll = false
      }
      this.delCartIds = this.selCartList.join(',')
    },
    // 包邮商品
    onChangeTwoSel(item, index) {
      // 判断是否能一起结算
      if (item.show) {
        this.selCartList.forEach((selItem, selIndex) => {
          if (selItem === item.cartId) {
            this.selCartList.splice(selIndex, 1)
          }
        })
      } else {
        this.selCartList.push(item.cartId)
      }
      item.show = !item.show
      this.number = this.selCartList.length
      if (this.compareArraySort(this.allCartList, this.selCartList)) {
        this.checkAll = true
      } else {
        this.checkAll = false
      }
    },
    // 全选/反选
    onCheckAll() {
      // 判断是否能全选
      if (this.checkAll) {
        if (this.listData.length) {
          this.listData.forEach(item => {
            item.show = false
          })
        }
        if (this.promotionList.length) {
          this.promotionList.forEach(listItem => {
            if (listItem.feeGoodsList) {
              listItem.feeGoodsList.forEach(itemOne => {
                itemOne.show = false
              })
            }
          })
        }
        this.selCartList = []
        this.number = 0
      } else {
        if (this.listData.length) {
          this.listData.forEach(item => {
            item.show = true
          })
        }
        if (this.promotionList.length) {
          this.promotionList.forEach(listItem => {
            if (listItem.feeGoodsList) {
              listItem.feeGoodsList.forEach(itemOne => {
                itemOne.show = true
              })
            }
          })
        }
        this.selCartList = this.allCartList
        this.number = this.selCartList.length
      }
      this.delCartIds = this.selCartList.join(',')
      this.checkAll = !this.checkAll
    },
    cartIndexEls() {
      let cartIndex = null
      this.navList = this.$pt.getStorageSync('navList')
      if (this.navList.length) {
        this.navList.forEach((item, index) => {
          if (item.linkType === 1007) {
            cartIndex = index
          }
        })
      } else {
        cartIndex = 1
      }
      return cartIndex
    },
    showFastNum(item) {
      let cartBadge = 0
      this.fastGoods.forEach(itemGoods => {
        if (itemGoods.goodsId === item.goodsId) {
          item.selShow = true
          if (itemGoods.skuFlag) {
            item.goodsNum = itemGoods.goodsNum
          } else {
            item.goodsNum = itemGoods.goodsNum
          }
        }
        cartBadge += itemGoods.goodsNum
      })
      if (this.cartIndexEls()) {
        this.$pt.setTabBarBadge({
          index: this.cartIndexEls(),
          text: String(cartBadge)
        })
      }
    },
    deleteShoppingCart(value) {
      const that = this
      this.$pt.showModal({
        content: '确定要删除此商品吗？',
        success(res) {
          if (res.confirm) {
            deleteShoppingCart(value).then((response) => {
              if (response.isError) return
              that.$utils.showToast({ title: '删除成功' })
              setTimeout(() => {
                that.getShopCart()
              }, 500)
              that.delCartIds = []
            }).catch(() => {
            })
          } else if (res.cancel) {
            console.log('用户取消')
          }
        }
      })
    },
    // 是否有选中的商品
    ifSelCart() {
      let selCart = false
      this.selCartList.forEach((item) => {
        if (item) selCart = true
      })
      return selCart
    },
    // 去结算
    settlement() {
      this.goodsListData = []
      // 判断是否超过限购数量
      if (!this.selCartList.length) return
      if (this.listData.length) {
        this.selCartList.forEach(item => {
          this.listData.forEach(listItem => {
            if (listItem.cartId === item) {
              this.goodsListData.unshift({
                goodsId: listItem.goodsId,
                skuId: listItem.skuId || '',
                goodsNum: listItem.goodsNum,
                cartId: listItem.cartId || ''
              })
            }
          })
        })
      }
      if (this.promotionList.length) {
        this.selCartList.forEach(item => {
          this.promotionList.forEach(listItem => {
            if (listItem.feeGoodsList) {
              listItem.feeGoodsList.forEach(itemOne => {
                if (itemOne.cartId === item) {
                  this.goodsListData.unshift({
                    goodsId: itemOne.goodsId,
                    skuId: itemOne.skuId || '',
                    goodsNum: itemOne.goodsNum,
                    cartId: itemOne.cartId || ''
                  })
                }
              })
            }
          })
        })
      }
      // this.submitEls()
      let params = {
        memberId: this.getMemberId,
        customerId: this.getCustomerId,
        addressId: '',
        roleType: this.getRoleType ? this.getRoleType : '',
        orderCheckGoodsList: this.goodsListData
      }
      doGetOrderCheck(params).then((response) => {
        if (response.isError) return
        this.submitEls()
      }).catch(() => {
      })
    },
    submitEls() {
      let overtake = this.listData.filter(item => {
        // 判断商品是否超过限购数
        return item.goodsNum > item.specimenLimitNum && item.specimenLimitNum > 0
      })
      if (overtake.length) {
        let overtakeresult = overtake.filter(item => {
          return this.selCartList.indexOf(item.cartId) > -1
        })
        if (overtakeresult.length) {
          return this.$utils.showToast({ title: `商品${overtakeresult[0].goodsTitle}超过限购数${overtakeresult[0].specimenLimitNum}` })
        }
      }
      if (this.selCartList.length === 0) {
        return
      }
      if (!this.ifSelCart()) {
        return this.$utils.showToast({ title: '请选择要结算的商品' })
      } else {
        if (this.shareMemberId) {
          this.$pt.navigateTo({
            url: '/pages/shop/buy/main?goodsList=' + JSON.stringify(this.goodsListData) + '&shareMemberId=' + this.shareMemberId
          })
          this.goodsListData = []
        } else {
          this.$pt.navigateTo({
            url: '/pages/shop/buy/main?goodsList=' + JSON.stringify(this.goodsListData)
          })
          this.goodsListData = []
        }
      }
    },
    onClose(value, event) {
      // 滑动单元格关闭事件
      const { position, instance } = event.mp.detail
      switch (position) {
        // 点击中间部分触发的事件
        case 'cell':
          instance.close()
          break
        case 'right':
          // 点击滑块右边触发的事件 (删除)
          this.deleteShoppingCart(value.cartId)
          instance.close()
          break
      }
    }
  }
}
</script>

<style lang="less" scoped>
.none-data{
  padding-top: 73px;
  .regBtn{
    background: #7427ff;
    color: #fff;
    height: 45px;
    padding: 0 90px;
    font-size: 16px;
    margin-top: 65px;
    border-radius: 100px;
  }
}
.fail-box{
  background: #fbfbfb;
  border-radius: 5px;
  margin: 0 10px;
  padding-bottom: 10px;
  overflow: hidden;
  .panel{
    margin-left: 0;
    margin-right: 0;
    margin-bottom: 0;
    border-radius: 0;
    box-shadow: 0 0 0;
    background-color: #fbfbfb;
    &:last-child{
      margin-bottom: 0;
    }
  }
}
.goods-num-bar{
  width: 24px;
  height: 24px;
  font-size: 12px;
  text-align: center;
  border-radius: 4px;
  border: #e6e6e6 1px solid;
}
.check-all{
  /deep/ .van-checkbox__label{
    color: #666;
  }
}
.top-space{
    height: 64px;
  }
.container {
  background: #f5f5f5;
  min-height: calc(100vh);
}
.cart-topbar{
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  background: #fff;
  width: 100%;
  box-sizing: border-box;
}
.fail-bar {
  text-align: center;
  height: 20px;
  border-top: #c5c5c5 1px solid;
  margin-top: 25px;
  margin-bottom: 5px;
  .fail-text {
    display: block;
    background: #f5f5f5;
    color: #999999;
    width: 73px;
    height: 20px;
    margin: -6px auto 0 auto;
    text-align: center;
    line-height: 1;
  }
}
.fail-tag-box {
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  .fail-tag {
    background: #999;
    color: #fff;
    text-align: center;
    height: 17px;
    line-height: 17px;
    border-radius: 50px;
  }
}
.cart-tab {
  background: #fff;
  position: fixed;
  left: 0;
  top: 0px;
  width: 100%;
  z-index: 20;
  .cart-tab-bar {
    width: 195px;
    margin: 0 auto;
  }
}
.font-red {
  color: #ff0000;
}
.icon-iconfontshanchu2 {
  color: #333;
}
.goods-title {
  // width: 173px;
  // height: unset;
  line-height: 18px;
  // overflow: hidden;
}
.panel {
  box-shadow: 0 0 0;
  border-top: none;
  border-bottom: none;
  overflow: hidden;
  border-radius: 5px;
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 15px;
  box-shadow: 0px 1px 6px 0px rgba(172, 172, 172, 0.1);
  .check-box-l {
    display: flex;
    align-items: center;
    padding-bottom: 10px;
  }
  /deep/ .van-swipe-cell__right {
    background: #ff4a4a;
    height: 100%;
    top: 0;
    color: white;
    width: 100%;
    padding-left: 13px;
    display: flex;
    align-items: center;
  }
}
.goods-prop {
  // width: 173px;  
  height: 22px;
  line-height: 22px;
  box-sizing: border-box;
  padding: 0 5px;
  margin-top: 0px;
  color: #999999;
  background: #f7f9fb;
  .goods-sku{
    width: 70px;
    height: 22px;
    line-height: 22px;
  }
  .iconfont{
    margin-left: 3px;
    transform:rotate(90deg);
  }
}

.img-item {
  width: 90px;
  height: 90px;
  border-radius: 5px;
}

.bottom-btn {
  width: 100%;
  height: 49px;
  position: fixed;
  z-index: 30;
  bottom: 0;
  background-color: #fff;  
  border-bottom: #f3f3f3 1px solid;
  box-shadow: 0px -3px 4px 0px rgba(206, 206, 206, 0.26);
}

.submit {
  padding: 0 10px 0 5px;
  .submit-btn-0{
    background: #e6e6e6!important;
    color: #999999!important;
    border: #e6e6e6 1px solid!important;
  }
  .submit-btn, .remove-btn {
    border-width: 1px;
    border-style: solid;
    border-radius: 50px;
    padding: 0 15px;
    height: 32px;
    // line-height: 32px;
    font-size: 15px;
  }
}
.shipping-goods {
  &:last-child {
    .goods-info {
      border-bottom: unset;
    }
  }
  .goods-info {
    border-bottom: 1px solid #f6f6f6;
  }
}
.goods-content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.goods-total {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  line-height: 1;
}
.shipping-icon {
  width: 29px;
  height: 16px;
  line-height: 16px;
  border-radius: 0 3px 3px;
  color: white;
  background: #ed4836;
  text-align: center;
}
.border-b {
  border-bottom: 1px solid #f6f6f6;
}
</style>
